import random

from tool.vector import Vector


def contains_numbers(arr, numbers):
    return all(num in set(arr) for num in numbers)


# 1. 升序排序
def OrderBy(arr, handler):
    arr.sort(key=handler)


# 2. 降序排序
def OrderByDescending(arr, handler):
    arr.sort(key=handler, reverse=True)


# 3. 返回最大的
def MaxArray(array, handler):
    return max(array, key=handler)


# 4. 返回最小的
def MinArray(array, handler):
    return min(array, key=handler)


# 5. 检查 slice 中某元素是否存在
def Find(slice, f):
    for i in slice:
        if f(i):
            return i
    return None


# 6. 查找所有满足条件的元素
def FindAll(array, handler):
    return [item for item in array if handler(item)]


# 7. 选择：选取数组中对象的某些成员形成一个独立的数组
def Select(array, handler):
    return [handler(item) for item in array]


# 8. 去重--复杂对象
def RemoveDuplicatesFromSlice(array, handler):
    seen = set()
    result = []
    for item in array:
        key = handler(item)
        if key not in seen:
            seen.add(key)
            result.append(item)
    return result


# 9. 去重
def RemoveDuplicatesFromSlice2(intSlice):
    seen = set()
    result = []
    for item in intSlice:
        if item not in seen:
            seen.add(item)
            result.append(item)
    return result


# 10. 随机打乱
def Shuffle(array):
    random.shuffle(array)
    return array


# 11. 判断两个切片是否完全一样
def CompareSlice(a, b):
    return a == b


# 12. 找到所有元素，指定元素删除并返回
def FindAllAndRemove(slice, handler):
    return [item for item in slice if not handler(item)]


# 13. 找到元素，指定元素删除并返回
def FindAndRemove(slice, handler):
    for i, item in enumerate(slice):
        if handler(item):
            return slice[:i] + slice[i + 1:], item
    return slice, None


# 14. 获取随机一个
def FindRand(slice):
    if len(slice) == 0:
        raise ValueError("slice is empty")
    return random.choice



def sort_enemies_by_distance(player_pot:Vector, enemies:[Vector]):
    # 计算每个敌人到玩家的距离
    enemies_with_distance = [(enemy,Vector.distance(player_pot,enemy)) for enemy in enemies]

    # 按距离从大到小排序
    sorted_enemies = sorted(enemies_with_distance, key=lambda x: x[1], reverse=True)

    # 返回排序后的敌人坐标
    return [enemy for enemy, distance in sorted_enemies]
